import Configurable
import Item
import Timekeeper

class Generator(Item.Item, Timekeeper.Timekeeper):
   runtime_vars = ('reserve',)
   
   __slots__ = ('recharge_rate',
                'capacity') + runtime_vars
   
   def __init__(self, recharge_rate, capacity, **args):
      Item.Item.__init__(self, **args)
      Configurable.Configurable.__init__(self, locals())
      self.reserve = float(self.capacity)
   
   def augmented_description(self):
      return \
         "%s\nCost: %d\nRecharge Rate: %s Capacity: %d\n\n%s" % \
         (self.name, self.value, 
          ("%s / sec" % str(self.recharge_rate)).ljust(11),
          self.capacity, self.description)
   
   def drain(self, amount):
      dr = min(self.reserve, amount)
      self.reserve -= dr
      if self.reserve < 0.0:
         self.reserve = 0.0
      return dr
   
   def update(self):
      if self.reserve < self.capacity:
         self.reserve += self.recharge_rate * self.frame()
         if self.reserve > self.capacity:
            self.reserve = self.capacity
